<?php

namespace Apps\AppApi\Method\User;

use Apps\Common\Libs\PicUrl;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\Keyword;
use Apps\Common\Libs\UploadPic;
use Apps\Common\Models\UserBase;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\SysAreaZipDistrictPhonecode;

class SetBaseInfo extends BaseMethod
{
    private $success = 0;
    private $message = '';

    public function run()
    {
        /**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }
        $area = ['user_cover','user_nickname','user_gender','user_birthday','user_realname','user_district','persional_sign'];

        $field = $this->getDataItem('field');//字段
        $value = trim($this->getDataItem('value', ''));//值

        if(!in_array($field, $area)){
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '字段名不存在.');
        }

        if ($user = UserBase::findFirst('user_id=' . $this->getUserAuth()->userId)) {
            if ($field == 'user_cover') // 头像
            {
                $up = new UploadPic();
                $user->user_cover = $up->userCover();
                if(!$user->user_cover) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '文件流不能为空.');
                }
                $value = PicUrl::UserCover($user->user_cover, $this->getDi());
            }
            elseif ($field == 'user_district') // 城市
            {
                if (!is_numeric($value)) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '城市格式错误.');
                }
                if (!\Apps\Common\Models\SysAreaZipDistrictPhonecode::count('K_ID = ' . $value)) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '城市不存在.');
                }
                $user->user_district = $value;
                // 获取城市信息
                $city = (new SysAreaZipDistrictPhonecode())->getOne($value);
                $value = $city['FULL_NAME'] ?: '';
            }
            elseif ($field == 'persional_sign') // 签名
            {
                if($value && $this->utf8_strlen($value) > 30){
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '个性签名字数超过了30个字符');
                }else{
                    $user->persional_sign = $value ? base64_encode(serialize($value)) : '';
                }
            }
            elseif ($field == 'user_nickname') // 昵称
            {
                $nicknameLength = $this->utf8_strlen($value);
                if ($nicknameLength < 1 || $nicknameLength > 10) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '昵称长度需在1~10字节之间');
                }
                $user->user_nickname = $value;
            }
            elseif ($field == 'user_realname') // 姓名
            {
                if (empty($value)) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '姓名不能为空');
                }
                if ($this->utf8_strlen($value) > 10) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '姓名不能超过10个字符');
                }
                $user->user_realname = $value;
            }
            elseif ($field == 'user_gender') // 性别
            {
                if (!in_array($value, ['0', '1', '2'])) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '请输入正确的性别');
                }
                $user->user_gender = $value;
            }
            elseif ($field == 'user_birthday') // 生日
            {
                if (!$this->validateDate($value)) {
                    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '日期格式错误');
                }
                $user->user_birthday = $value;
            }
            if ($user->save()) {
                $this->setResult(['success' => 1, 'message' => $field . ' 更新成功', 'value' => $value]);
                return true;
            }
        }
        $this->setResult([
            'success' => 0,
            'message' => '更新失败',
            'value' => ''
        ]);
    }

    /**
     * 检查日期格式是否正确
     * @param string $date date格式
     * @return bool
     */
    public function validateDate($date)
    {
        $date = explode('-', $date);
        return checkdate($date['1'], $date['2'], $date['0']);
    }


}